package com.wo56.pushmsgcenter.common.exe;

import java.util.Iterator;

import com.framework.components.redis.RedisHelper;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.ShardedJedis;

public class TestRedisClient {
	public static void main(String[] args) throws Exception {
		Jedis jedis = null;
		if(RedisHelper.getInstance().isShared()){
			ShardedJedis shardedJedis = (ShardedJedis) RedisHelper.getInstance().getJedis();
			Iterator it = shardedJedis.getAllShards().iterator();
			//分片的话拿第1台做消息服务
			jedis = (Jedis) it.next();
		}else{
			jedis = (Jedis) RedisHelper.getInstance().getJedis();
		}
		jedis.subscribe(new TestRedisClient().new RedisMsgPubSubListener(), "test");
		
	}
	
	public class RedisMsgPubSubListener extends JedisPubSub {

		@Override
		public void onMessage(String channel, String message) {
			System.out.println("channel:" + channel + "-->receives message :" + message); 
		}

		@Override
		public void onPMessage(String pattern, String channel, String message) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSubscribe(String channel, int subscribedChannels) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onUnsubscribe(String channel, int subscribedChannels) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPUnsubscribe(String pattern, int subscribedChannels) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPSubscribe(String pattern, int subscribedChannels) {
			// TODO Auto-generated method stub
			
		}  
		
	}
}
